package com.yunshang.shoppingmall.shopping.productApi.controller;

import com.yunshang.shoppingmall.common.HttpRequest;
import com.yunshang.shoppingmall.entity.*;
import com.yunshang.shoppingmall.service.*;
import com.yunshang.shoppingmall.service.User.UserRankService;
import com.yunshang.shoppingmall.service.impl.ProductApisServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.*;

@Controller
@CrossOrigin
@RequestMapping("/wap")
public class ProductApisController {

    @Autowired
    ProductApisService productApisService;

    @Autowired
    UserRankService userRankService;

    @Autowired
    ProductPriceService productPriceService;

    @Autowired
    ProductGuiGeService productGuiGeService;

    @Autowired
    ProductCommentService productCommentService;

    ProductApisServiceImpl productApisServiceImpl = new ProductApisServiceImpl();
    //前端商品详情页
    @Autowired
    CollectService collectService;
    @Autowired
    ProductService productService;




    /**
     * 从session中获取登录用户的提成比
     *
     * @param session
     * @return
     */
    public double getCommission(HttpSession session) {
        double commission = 0.00;
        if (session != null) {
            UserNo userNo = (UserNo) session.getAttribute("userNo");
//            int rankId=userNo.getRank();
            int rankId = 18;
            if (rankId != 0) {
                //根据userId查询用户等级和提成比
                UserRank userRank = userRankService.selectByPrimaryKey(rankId);
                if ("优质店主".equals(userRank.getRankname())) {
                    commission = userRank.getCommission();//提成比
                }
            }
        }
        return commission;
    }

    @ResponseBody
    @RequestMapping(value = "/selectGuiGeById", method = RequestMethod.POST)
    public Map<String, Object> selectGuiGeById(HttpServletRequest request) {
        double priceOpratorCosts = 0.00;
        double coupons = 0.00;
        int yzDianzhu = 0;
        double yzDianzhus = 0.00;
        double productCosts = 0.00;
        double YZDZcommission = 80.00;
        double HYcommission = 30.00;
        double proPrices = 0.00;
        Map<String, Object> map = new HashMap<>();
        String[] imgGroups = new String[5];
        String guigeId = request.getParameter("guigeId");
        if (guigeId != "" && guigeId != null) {
            int guigeIds = Integer.parseInt(guigeId);
            ProductGuiGe productGuiGe = productGuiGeService.selectByPrimaryKey(guigeIds);//查询规格
            //根据规格ID查询价格
            ProductPrice productPrice = productPriceService.selectProductPriceByGuigeId(guigeIds);
            BigDecimal proPrice = productPrice.getProPrice();
            if (proPrice != null) {
                proPrices = proPrice.doubleValue();
            }
            BigDecimal productCost = productPrice.getProductCost();//商品成本
            if (productCost != null) {
                productCosts = productCost.doubleValue();
            }

            BigDecimal priceOpratorCost = productPrice.getOpratorCost();//运营成本
            if (priceOpratorCost != null) {
                priceOpratorCosts = priceOpratorCost.doubleValue();
            }
            BigDecimal coupon = productPrice.getCoupon();//兑换券
            if (coupon != null) {
                coupons = coupon.doubleValue();
            }

            yzDianzhu = productPrice.getYzDianzhu();//优质店主折扣券
            yzDianzhus = (double) yzDianzhu;
            double fenXiangZhuan = productApisServiceImpl.getFenXiangZhuan(proPrices, productCosts, priceOpratorCosts, coupons, yzDianzhus, YZDZcommission, HYcommission);
            map.put("productPrice", productPrice);
            map.put("productGuiGe", productGuiGe);
            map.put("fenxiangZhuan", fenXiangZhuan);
            imgGroups = productGuiGe.getImgGroup().split(",");
            map.put("imgGroups", imgGroups);
        }
        return map;
    }

    //商品收藏
    @ResponseBody
    @RequestMapping("/proAddCollect")
    public Map<String, Object> proAddCollect(HttpServletRequest request) {

        Map<String, Object> map = new HashMap<>();
        HttpSession session = request.getSession();
        UserNo userNo1 = (UserNo) session.getAttribute("userNo");
        //登录
        if (userNo1 != null) {
            String productId = request.getParameter("productId");
            int proId = 0;
            double proPrice = 0.00;
            String productPrice = request.getParameter("productPrice");

            if (productId != null && productId != "") {
                proId = Integer.parseInt(productId);
            }
            if (productPrice != null && productPrice != "") {
                proPrice = Double.parseDouble(productPrice);
            }
            List<Collect> collectList = collectService.selectByUserNoIdAndProId(userNo1.getId(), proId);
            if (collectList != null && collectList.size() != 0) {
                map.put("msg", '1');
                map.put("loginMsg", userNo1);
                return map;
            }
            Product product = productService.selectByPrimaryKey(proId);
            Collect collect = new Collect();
            if (product != null) {
                collect.setProductDesc(product.getProductDesc());
                collect.setProductId(proId);
                collect.setProductImgUrl(product.getProImg());
                if (userNo1 != null) {
                    collect.setUserId(userNo1.getId());
                }
                collect.setProductPrice(proPrice);
            }
            int row = collectService.insertSelective(collect);
            if (row > 0) {
                map.put("msg", '1');//保存成功！
//                List<Collect> collectList=collectService.selectAllByUserNoId(userNo1.getId());
//                map.put("collecSize",collectList.size());
            } else {
                map.put("msg", '0');
            }

            map.put("loginMsg", userNo1);
        } else {
            map.put("loginMsg", userNo1);
        }
        return map;
    }

}
